#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include <unistd.h>
#include "log.hpp"
static const std::string sep=": ";
class dict
{
public:
	dict(const std::string path)
		:_path(path)
	{
		std::ifstream in(path);
		if(!in.is_open())
		{
			LOG(FATAL,"open %s err\n",path.c_str());
			exit(6);
		};
		std::string line;
		while(std::getline(in,line))
		{
			//std::cout<<"begin getline"<<std::endl;

			size_t pos=line.find(sep);
			if(pos==std::string::npos)
				continue;
			std::string key =line.substr(0,pos);//左闭右开
			if(key.empty())//简单判断一下,并不全面
				continue;
			std::string value=line.substr(pos+sep.size());//第二个参数默认为npos
			if(value.empty())
				continue;
			_data.insert(std::make_pair(key,value));
			LOG(DEBUG,"%s load success",line.c_str());
		};
		in.close();

	};
	std::string Translate(std::string word)
	{
		if(word.empty())//简单判断一下
		return	"None";
		auto iter =_data.find(word);
		return iter==_data.end()?"None":iter->second;

	};

private:
	std::unordered_map<std::string,std::string> _data;
		
	std::string _path;
};
